/**
    @author: dt
    @date: 2025-06-23
    @description: 行为树节点基类
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLib.BehaviorTree
{
    /// <summary>
    /// 行为树节点状态枚举
    /// </summary>
    public enum NodeState
    {
        /// <summary>节点正在执行中</summary>
        Running,
        /// <summary>节点执行成功</summary>
        Success,
        /// <summary>节点执行失败</summary>
        Failure,
        /// <summary>节点被中断</summary>
        Interrupted
    }

    /// <summary>
    /// 行为树节点基类
    /// 所有行为树节点都继承自此类
    /// </summary>
    public abstract class Node
    {
        /// <summary>当前节点状态</summary>
        protected NodeState state;

        /// <summary>父节点引用</summary>
        public Node parent;

        /// <summary>子节点列表</summary>
        protected List<Node> children = new List<Node>();

        /// <summary>是否被中断</summary>
        protected bool isInterrupted = false;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Node()
        {
            parent = null;
        }

        /// <summary>
        /// 带子节点的构造函数
        /// </summary>
        /// <param name="children">子节点列表</param>
        public Node(List<Node> children)
        {
            foreach (Node child in children)
            {
                Attach(child);
            }
        }

        /// <summary>
        /// 将节点附加为子节点
        /// </summary>
        /// <param name="node">要附加的节点</param>
        private void Attach(Node node)
        {
            node.parent = this;
            children.Add(node);
        }

        /// <summary>
        /// 评估节点状态
        /// 子类必须重写此方法来实现具体的逻辑
        /// </summary>
        /// <returns>节点执行状态</returns>
        public virtual NodeState Evaluate() => NodeState.Failure;

        /// <summary>
        /// 中断当前节点及其所有子节点
        /// </summary>
        public virtual void Interrupt()
        {
            isInterrupted = true;
            state = NodeState.Interrupted;
            
            // 中断所有子节点
            foreach (Node child in children)
            {
                child.Interrupt();
            }
        }

        /// <summary>
        /// 重置节点状态
        /// </summary>
        public virtual void Reset()
        {
            isInterrupted = false;
            state = NodeState.Failure;
            
            // 重置所有子节点
            foreach (Node child in children)
            {
                child.Reset();
            }
        }

        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="child">要添加的子节点</param>
        public void AddChild(Node child)
        {
            child.parent = this;
            children.Add(child);
        }

        /// <summary>
        /// 移除子节点
        /// </summary>
        /// <param name="child">要移除的子节点</param>
        public void RemoveChild(Node child)
        {
            children.Remove(child);
            child.parent = null;
        }

        /// <summary>
        /// 获取所有子节点
        /// </summary>
        /// <returns>子节点列表</returns>
        public List<Node> GetChildren()
        {
            return children;
        }

        /// <summary>
        /// 获取当前节点状态
        /// </summary>
        /// <returns>节点状态</returns>
        public NodeState GetState()
        {
            return state;
        }
    }
}
